<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button onclick="add();">添加节点</button>
    <ul id="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>


    <script>
        var num = 4;

        function add() {
            // 创建li节点
            var $li = document.createElement('li');

            $li.innerText = num++;
            console.log(num);

            // 把li节点插入到ul里面
            var $list = document.querySelector('#list');
            $list.appendChild($li);
        }
    </script>
</body>

</html>